package com.li.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.li.entity.PageResult;
import com.li.entity.QueryPageBean;
import com.li.mapper.CheckGroupMapper;
import com.li.pojo.CheckGroup;
import com.li.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService{
    @Autowired
    CheckGroupMapper checkGroupMapper;
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkItemId) {

        //插入检查组
        checkGroupMapper.addCheckGroup(checkGroup);
        //关联检查组与检查项
        setCheckGroupAndCheckItem(checkItemId,checkGroup.getId());
    }

    @Override
    public PageResult findPageByCondition(QueryPageBean queryPageBean) {

        //分页查询 开启分页

        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());

        Page<CheckGroup> pages = checkGroupMapper.findPageByCondition(queryPageBean.getQueryString());

        //设置分页数据
        PageResult result = new PageResult();

        result.setTotal(pages.getTotal());

        result.setRows(pages.getResult());

        return result;
    }

    @Override
    public void delete(Integer id) {

        try{
            //删除检查套餐与检查组的关联
            checkGroupMapper.deleteSetmealAndCheckGroupById(id);
            //删除检查组与检查项的关联
            checkGroupMapper.deleteCheckGroupAndCheckItemById(id);
            //删除检查组
            checkGroupMapper.deleteCheckGroupById(id);
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    @Override
    public CheckGroup findOne(Integer id) {

        return checkGroupMapper.findOne(id);
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {

        return checkGroupMapper.findCheckItemIdsByCheckGroupId(id);
    }

    @Override
    public void edit(Integer[] ids, CheckGroup checkGroup) {

        //删除检查组与检查项的关联
        checkGroupMapper.deleteCheckGroupAndCheckItemById(checkGroup.getId());
        //建立检查组和检查项现有关系
        setCheckGroupAndCheckItem(ids,checkGroup.getId());
        //更新检查组
        checkGroupMapper.edit(checkGroup);
    }

    @Override
    public List<CheckGroup> findAll() {

        return checkGroupMapper.findAll();
    }

    public void setCheckGroupAndCheckItem(Integer[] checkItemId,Integer id){
        if(checkItemId!=null&&checkItemId.length>0){
            //获取id成功 进行关联

            for (Integer integer : checkItemId) {

                HashMap<String, Integer> map = new HashMap<>();
                //获取检查组id和检查项id进行关联
                map.put("checkGroupId",id);
                map.put("checkItemId",integer);

                checkGroupMapper.addCheckGroupIdAndCheckItemId(map);
            }
        }
    }
}